package research.ui.views;

import java.util.Calendar;
import java.util.List;

import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;

import research.entity.EntityType;
import research.model.ListHierarchy;
import research.ui.views.comparators.ApplicationDateComparator;
import research.ui.views.comparators.ApplicationDesiredDateComparator;
import research.ui.views.filters.ApplicationDateFilter;
import research.ui.views.filters.ApplicationDesiredDateFilter;

public class ApplicationView extends HierarchyView {
	public static final String ID = "research.ui.views.ApplicationView";
	
	private DateTime start;
	private DateTime finish;
	private DateTime startDesired;
	private DateTime finishDesired;
	
	public ApplicationView() {
		super(new ListHierarchy(EntityType.Application));
	}

	@Override
	protected void renderAdditionalFilters(Composite parent) {
		super.renderAdditionalFilters(parent);
		
		FormToolkit toolkit = new FormToolkit(parent.getDisplay());

		Section section = toolkit.createSection(parent, Section.EXPANDED
				| Section.TITLE_BAR | Section.TWISTIE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		section.setLayoutData(gd);
		section.setText("По дате составления");
		Composite composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		Label label = toolkit.createLabel(composite, "От:");
		label.setLayoutData(gd);
		gd = new GridData();
		start = new DateTime(composite, SWT.DATE);
		start.setLayoutData(gd);

		gd = new GridData();
		label = toolkit.createLabel(composite, "До:");
		label.setLayoutData(gd);
		gd = new GridData();
		finish = new DateTime(composite, SWT.DATE);
		finish.setLayoutData(gd);
		
		section = toolkit.createSection(parent, Section.EXPANDED
				| Section.TITLE_BAR | Section.TWISTIE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		section.setLayoutData(gd);
		section.setText("По дате проведения");
		composite = toolkit.createComposite(section);
		composite.setLayout(new GridLayout(2, false));
		section.setClient(composite);
		gd = new GridData();
		label = toolkit.createLabel(composite, "От:");
		label.setLayoutData(gd);
		gd = new GridData();
		startDesired = new DateTime(composite, SWT.DATE);
		startDesired.setLayoutData(gd);

		gd = new GridData();
		label = toolkit.createLabel(composite, "До:");
		label.setLayoutData(gd);
		gd = new GridData();
		finishDesired = new DateTime(composite, SWT.DATE);
		finishDesired.setLayoutData(gd);
	
	}
	
	@Override
	protected ViewerComparator getComparator() {
		return new ApplicationDateComparator();
	}

	@Override
	protected void addAdditionalFilters(List<ViewerFilter> filters) {
		super.addAdditionalFilters(filters);
		Calendar sdate = Calendar.getInstance();
		sdate.set(start.getYear(), start.getMonth(), start.getDay(), start
				.getHours(), start.getMinutes(), start.getSeconds());

		Calendar fdate = Calendar.getInstance();
		fdate.set(finish.getYear(), finish.getMonth(), finish.getDay(), finish
				.getHours(), finish.getMinutes(), finish.getSeconds());
		
		filters.add(new ApplicationDateFilter(sdate.getTime(), fdate.getTime()));
		
		sdate = Calendar.getInstance();
		sdate.set(startDesired.getYear(), startDesired.getMonth(), startDesired.getDay(), startDesired
				.getHours(), startDesired.getMinutes(), startDesired.getSeconds());

		fdate = Calendar.getInstance();
		fdate.set(finishDesired.getYear(), finishDesired.getMonth(), finishDesired.getDay(), finishDesired
				.getHours(), finishDesired.getMinutes(), finishDesired.getSeconds());
		
		filters.add(new ApplicationDesiredDateFilter(sdate.getTime(), fdate.getTime()));
	}
	
	@Override
	protected void renderAdditionalSorters(Composite parent) {
		super.renderAdditionalSorters(parent);
		
		GridData gd = new GridData();
		Button sortName = new Button(parent, SWT.NONE);
		sortName.setText("По дате составления");
		sortName.setLayoutData(gd);
		sortName.addSelectionListener(new SortBtnSelectionListener(new ApplicationDateComparator()));
		
		gd = new GridData();
		sortName = new Button(parent, SWT.NONE);
		sortName.setText("По дате проведения");
		sortName.setLayoutData(gd);
		sortName.addSelectionListener(new SortBtnSelectionListener(new ApplicationDesiredDateComparator()));
	
	}
}